package com.common.base.app.extras

import android.graphics.Color
import android.net.Uri
import android.text.TextUtils
import java.lang.StringBuilder
import java.util.regex.Matcher
import java.util.regex.Pattern

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/8/20
 * <p>
 * 描述：
 * <p>
 * 修订历史：
 */
fun String?.isEmpty(): Boolean {
    if (TextUtils.isEmpty(this) || this == "null") {
        return true
    }
    return false
}

fun String?.isNoEmpty(): Boolean {
    return !this.isEmpty()
}

fun String?.getUrl(): String? {
    var path = this
    if (path?.startsWith("http") == false) {
        path = "http:$this"
    }
    return path
}

//获取字符串中的数字
fun String?.getNumber(): String? {
    val regEx = "[^0-9]"
    val p = Pattern.compile(regEx)
    val m = p.matcher(this ?: "")
    return m.replaceAll("")
}

fun String?.getString(def: String = ""): String {
    this.isEmpty().yes { return def }
    return this ?: def
}

//获取最大长度的字符串
fun String?.getStringMaxLength(length: Int): String {
    if (this?.length ?: 0 > length) {
        return this?.substring(0, length) ?: ""
    }
    return this ?: ""
}

fun String?.getNoZeroString(): String {
    if (this.isEmpty()) {
        return this.getString()
    } else if (this == "0.0" || this == "0.00" || this == "0") {
        return ""
    }
    return this ?: ""
}

//判断是否是数字
fun isNumeric(str: Any?): Boolean {
    if (str == null || isEmpty(str.toString())) {
        return false
    }
    val pattern: Pattern = Pattern.compile("[0-9]*")
    val isNum: Matcher = pattern.matcher(str.toString())
    return isNum.matches()
}

/**
 * 将string类型颜色
 */
fun parseColor(colorStr: String?, def: String = "#666666"): Int {
    return try {
        return Color.parseColor(colorStr)
    } catch (e: Exception) {
        Color.parseColor(def)
    }
}

fun formatNumber(x: Float, z: Int): String {
    return String.format("%." + z + "f", x)
}

fun formatNumber(x: Double, z: Int): String {
    return String.format("%." + z + "f", x)
}

fun String.getHidePhone(): String {
    var phone = this
    if (this.isNullOrEmpty().not()) {
        if (this.length == 11){
            val middle = this.substring(3, 7)
            phone = this.replace(middle, "****")
        }else if (this.length >= 7){
            //隐藏的数字个数 除去前三后三
            val hideNum = this.length - 6
            val sb = StringBuilder()
            for (i in 0 until hideNum){
                //拼接多少个*
                sb.append("*")
            }
            val middle = this.substring(3,this.length - 3)
            phone = this.replace(middle,sb.toString())
        }
    }
    return phone
}

/***
 * 检查用户名格式是否正确
 * 4-11 数字和字母组合
 * @return
 */
fun String.isMatchUsernameFormat(): Boolean {
    if (this.isNullOrEmpty())
        return false

    val pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,11}$"
    return Pattern.compile(pattern).matcher(this).matches()
}

/***
 * 检查密码格式是否正确
 * 6-12 数字和字母组合
 * @return
 */
fun String.isMatchPwdFormat(): Boolean {
    if (this.isNullOrEmpty())
        return false

    val pattern = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$"
    return Pattern.compile(pattern).matcher(this).matches()
}

/**
 * 获取url中某个参数值
 */
fun String.getUrlParamValue(paramName: String): String {
    var result = ""
    try {
        val uri = Uri.parse(this)
        result = uri.getQueryParameter(paramName) ?: ""
    } catch (e: Exception) {
        e.printStackTrace()
    }
    return result
}

/**
 * 仅移除空格和换行
 */
fun String.removeSpacesAndNewlines(): String = replace(Regex("[ \\n\\r]+"), "")

/**
 * 移除所有空白字符
 */
fun String.removeAllWhitespace(): String = replace(Regex("\\s+"), "")